package controller

import (
	"awesomeProject1/微信登录demo/baseBean"
	"awesomeProject1/微信登录demo/dao"
	"awesomeProject1/微信登录demo/models"
	"awesomeProject1/微信登录demo/utils"
	"github.com/gin-gonic/gin"
	"log"
)

func GetUserPhoneHandler(c *gin.Context) {
	var mUserPhone models.UserPhone
	err := c.ShouldBind(&mUserPhone)
	if err != nil {
		baseBean.Fail(c, "json序列化格式错误: "+err.Error(), nil)
		return
	}
	log.Println("解密前解析前：",mUserPhone)
	log.Println("解密前解析后：",mUserPhone.EncryptedData)
	log.Println("解密前解析后：",mUserPhone.Iv)


	strSql:= `select sessionKey from wxlogin2 where openid = ?`
	var vv []models.LoginModels
	err = dao.DB.Raw(strSql, "oJG2n5GIcuEGunZq0EcUugIEGXpc").Scan(&vv).Error
	if err != nil {
		baseBean.Fail(c, "查询出错: "+err.Error(), nil)
		return
	}

	if vv[0].SessionKey != "" {
		//调用解密
		data, err := utils.DecryptWXOpenData(vv[0].SessionKey, mUserPhone.EncryptedData, mUserPhone.Iv)
		if err != nil {
			baseBean.Fail(c, "128解密出错："+err.Error(), nil)
			return
		}

		baseBean.Success(c,"获取手机号码成功",data)

	}



}










